/**
 * 
 */
package com.pos;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.pos.net.request.StringNetRequest;

/**
 * @description:基础fragment，实现此类，必要时候必须在initView()中对parentActivity强转或者重新定义。
 * @author cai
 * @time:2015年11月27日下午7:07:55
 */
public abstract class BaseFragment extends Fragment {
	/**
	 * onCreateView返回的主view
	 */
	public View parentView;
	/**
	 * fragment的主activity
	 */
	public Activity parentActivity;
	/**
	 * log打印需要用到的标签
	 */
	public String Tag = "BaseFragment";

	/**
	 * 传入参数为字符串拼接方式的网络请求实例
	 */
	public StringNetRequest netRequest;

	public ProgressDialog dialog;

	@SuppressWarnings("unchecked")
	protected <T extends View> T bindId(int id) {
		return (T) parentView.findViewById(id);
	}

	@SuppressWarnings("unchecked")
	protected <T extends View> T bindId(View view, int id) {
		return (T) view.findViewById(id);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		parentActivity = getActivity();
		parentView = inflater.inflate(initLayoutId(), container, false);
		initView();
		Tag = this.getClass().getSimpleName();
		netRequest = StringNetRequest.getInstance(parentActivity);
		initData();
		initListener();
		return parentView;
	}

	/**
	 * 初始化设置本fragment layout id
	 * 
	 * @return int layoutId
	 */
	public abstract int initLayoutId();

	/**
	 * 初始化视图,放在getlayoutId()方法后面
	 */
	public abstract void initView();

	/**
	 * 初始化数据
	 */
	public abstract void initData();

	/**
	 * 初始化监听事件
	 */
	public abstract void initListener();

	/**
	 * toast短提示
	 * 
	 * @param text
	 *            提示语
	 */
	public void showShortToast(String text) {
		Toast.makeText(parentActivity, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * toast短提示
	 * 
	 * @param stringId
	 *            string资源
	 */
	public void showShortToast(int stringId) {
		Toast.makeText(parentActivity, stringId, Toast.LENGTH_SHORT).show();
	}

	/**
	 * toast长提示
	 * 
	 * @param text
	 *            提示语
	 */
	public void showLongToast(String text) {
		Toast.makeText(parentActivity, text, Toast.LENGTH_LONG).show();
	}

	/**
	 * toast长提示
	 * 
	 * @param stringId
	 *            string资源
	 */
	public void showLongToast(int stringId) {
		Toast.makeText(parentActivity, stringId, Toast.LENGTH_LONG).show();
	}

	/**
	 * startActivity
	 * 
	 * @param activityClass
	 *            即将启动的activity类
	 */
	public void startActivity(Class<? extends Activity> activityClass) {
		Intent intent = new Intent(parentActivity, activityClass);
		parentActivity.startActivity(intent);
	}

	public void showDialg() {
		if (dialog == null) {
			dialog = new ProgressDialog(getActivity());
		}
		dialog.setMessage(parentActivity.getString(R.string.string_progress_message));
		// dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.show();
	}

	public void disMissDialog() {
		if (dialog != null) {
			dialog.dismiss();
		}
	}
}
